USE [master]
GO
/****** Object:  Database [SmartProject]    Script Date: 12/06/2009 13:42:21 ******/
CREATE DATABASE [SmartProject] ON  PRIMARY 
( NAME = N'SmartProject', FILENAME = N'C:\Share\Database\Development\SmartProject.mdf' , SIZE = 2240KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
 LOG ON 
( NAME = N'SmartProject_log', FILENAME = N'C:\Share\Database\Development\SmartProject_log.LDF' , SIZE = 832KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
 COLLATE SQL_Latin1_General_CP1_CI_AS
GO
EXEC dbo.sp_dbcmptlevel @dbname=N'SmartProject', @new_cmptlevel=90
GO
IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
begin
EXEC [SmartProject].[dbo].[sp_fulltext_database] @action = 'enable'
end
GO
ALTER DATABASE [SmartProject] SET ANSI_NULL_DEFAULT OFF 
GO
ALTER DATABASE [SmartProject] SET ANSI_NULLS OFF 
GO
ALTER DATABASE [SmartProject] SET ANSI_PADDING OFF 
GO
ALTER DATABASE [SmartProject] SET ANSI_WARNINGS OFF 
GO
ALTER DATABASE [SmartProject] SET ARITHABORT OFF 
GO
ALTER DATABASE [SmartProject] SET AUTO_CLOSE ON 
GO
ALTER DATABASE [SmartProject] SET AUTO_CREATE_STATISTICS ON 
GO
ALTER DATABASE [SmartProject] SET AUTO_SHRINK ON 
GO
ALTER DATABASE [SmartProject] SET AUTO_UPDATE_STATISTICS ON 
GO
ALTER DATABASE [SmartProject] SET CURSOR_CLOSE_ON_COMMIT OFF 
GO
ALTER DATABASE [SmartProject] SET CURSOR_DEFAULT  GLOBAL 
GO
ALTER DATABASE [SmartProject] SET CONCAT_NULL_YIELDS_NULL OFF 
GO
ALTER DATABASE [SmartProject] SET NUMERIC_ROUNDABORT OFF 
GO
ALTER DATABASE [SmartProject] SET QUOTED_IDENTIFIER OFF 
GO
ALTER DATABASE [SmartProject] SET RECURSIVE_TRIGGERS OFF 
GO
ALTER DATABASE [SmartProject] SET  DISABLE_BROKER 
GO
ALTER DATABASE [SmartProject] SET AUTO_UPDATE_STATISTICS_ASYNC OFF 
GO
ALTER DATABASE [SmartProject] SET DATE_CORRELATION_OPTIMIZATION OFF 
GO
ALTER DATABASE [SmartProject] SET TRUSTWORTHY OFF 
GO
ALTER DATABASE [SmartProject] SET ALLOW_SNAPSHOT_ISOLATION OFF 
GO
ALTER DATABASE [SmartProject] SET PARAMETERIZATION SIMPLE 
GO
ALTER DATABASE [SmartProject] SET  READ_WRITE 
GO
ALTER DATABASE [SmartProject] SET RECOVERY SIMPLE 
GO
ALTER DATABASE [SmartProject] SET  MULTI_USER 
GO
ALTER DATABASE [SmartProject] SET PAGE_VERIFY CHECKSUM  
GO
ALTER DATABASE [SmartProject] SET DB_CHAINING OFF 
USE [SmartProject]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[smartproject_SystemPreference]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[smartproject_SystemPreference](
	[SystemPrefID] [int] IDENTITY(1,1) NOT NULL,
	[PrefName] [varchar](200) NOT NULL,
	[PrefValue] [varchar](200) NOT NULL,
	[Active] [bit] NOT NULL CONSTRAINT [DF_SystemPreference_Active]  DEFAULT ((1)),
 CONSTRAINT [PK_SystemPreference] PRIMARY KEY CLUSTERED 
(
	[SystemPrefID] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[smartproject_User]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[smartproject_User](
	[UserId] [int] IDENTITY(1,1) NOT NULL,
	[UserName] [varchar](300) NOT NULL,
	[FirstName] [varchar](200) NOT NULL,
	[LastName] [varchar](150) NOT NULL,
	[EMail] [varchar](250) NOT NULL,
	[Active] [bit] NOT NULL CONSTRAINT [DF_User_Active]  DEFAULT ((1)),
 CONSTRAINT [PK_User] PRIMARY KEY CLUSTERED 
(
	[UserId] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[smartproject_Log]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[smartproject_Log](
	[LogId] [int] IDENTITY(1,1) NOT NULL,
	[PageOrClassName] [varchar](50) NOT NULL,
	[ActionType] [char](3) NOT NULL,
	[Description] [varchar](500) NOT NULL,
	[SeverityLevel] [char](3) NOT NULL,
	[Created] [datetime] NOT NULL,
 CONSTRAINT [PK_Log] PRIMARY KEY CLUSTERED 
(
	[LogId] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[smartproject_GetRoles]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'-- =============================================
-- Author:		David Lee
-- Create date: 11/1/2009
-- Description:	Selects all roles from the datbase
-- =============================================
CREATE PROCEDURE [dbo].[smartproject_GetRoles]
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	SELECT * FROM [smartproject_Role];
END

' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[smartproject_Work]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[smartproject_Work](
	[WorkId] [int] IDENTITY(1,1) NOT NULL,
	[OwnerId] [int] NOT NULL,
	[DeveloperId] [int] NULL,
	[ProductId] [int] NOT NULL,
	[SprintId] [int] NULL,
	[Description] [varchar](200) NOT NULL,
	[Priority] [smallint] NOT NULL,
	[DueDate] [smalldatetime] NOT NULL,
	[Active] [bit] NOT NULL CONSTRAINT [DF_Work_Active]  DEFAULT ((1)),
	[TypeCode] [char](3) NOT NULL,
	[Assigned] [smalldatetime] NULL,
 CONSTRAINT [PK_Work] PRIMARY KEY CLUSTERED 
(
	[WorkId] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[smartproject_WorkHistory]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[smartproject_WorkHistory](
	[WorkHistoryId] [int] IDENTITY(1,1) NOT NULL,
	[ActivityCode] [char](3) NOT NULL,
	[ByOwnerId] [int] NOT NULL,
	[ToDeveloperId] [int] NULL,
	[Created] [datetime] NOT NULL,
	[WorkId] [int] NOT NULL,
 CONSTRAINT [PK_WorkHistory] PRIMARY KEY CLUSTERED 
(
	[WorkHistoryId] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[smartproject_Sprint]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[smartproject_Sprint](
	[SprintId] [int] IDENTITY(1,1) NOT NULL,
	[Description] [varchar](200) NOT NULL,
	[Started] [smalldatetime] NULL,
	[EstimatedDays] [int] NOT NULL,
	[Ended] [smalldatetime] NULL,
	[OwnerId] [int] NOT NULL,
 CONSTRAINT [PK_Sprint] PRIMARY KEY CLUSTERED 
(
	[SprintId] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[smartproject_Product]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[smartproject_Product](
	[ProductId] [int] IDENTITY(1,1) NOT NULL,
	[Description] [varchar](150) NOT NULL,
	[Created] [smalldatetime] NOT NULL,
	[Active] [bit] NOT NULL,
	[OwnerId] [int] NOT NULL,
 CONSTRAINT [PK_Product] PRIMARY KEY CLUSTERED 
(
	[ProductId] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[smartproject_GetProducts]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[smartproject_GetProducts]
AS
	SET NOCOUNT ON
	SELECT [ProductId], [Description] FROM smartproject_PRODUCT WHERE [Active] = 1
	RETURN

' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[smartproject_AddProduct]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[smartproject_AddProduct]
	(@Description	varchar(150)
	,@OwnerId		int)
AS
	INSERT INTO [smartproject_Product] ([Description], [OwnerId], [Created])
	VALUES (@Description, @OwnerId, CURRENT_TIMESTAMP);
	RETURN @@IDENTITY;

' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[smartproject_AddWork]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[smartproject_AddWork]
	(@OwnerId		int
	,@DeveloperId	int
	,@ProductId		int
	,@Description	varchar(200)
	,@Priority		smallint
	,@DueDate		smalldatetime
	,@TypeCode		char(3))
AS
	INSERT INTO [smartproject_Work] ([OwnerId], [DeveloperId], [ProductId], [Description], [Priority], [DueDate], [TypeCode])
	VALUES (@OwnerId, @DeveloperId, @ProductId, @Description, @Priority, @DueDate, @TypeCode);
	RETURN @@IDENTITY;

' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[smartproject_AddSystemPreference]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'-- =============================================
-- Author:		David Lee
-- Create date: 11/2/2009
-- Description:	Add System Preference
-- =============================================
CREATE PROCEDURE [dbo].[smartproject_AddSystemPreference]
	-- Add the parameters for the stored procedure here
(@PrefName		VARCHAR(200)
,@PrefValue		VARCHAR(200))
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	INSERT INTO smartproject_SystemPreference(PrefName, PrefValue) VALUES (@PrefName, @PrefValue);
END

' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[smartproject_GetSystemPreference]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'-- =============================================
-- Author:		David Lee
-- Create date: 11/2/2009
-- Description:	Get System Preference
-- =============================================
CREATE PROCEDURE [dbo].[smartproject_GetSystemPreference]
	-- Add the parameters for the stored procedure here
(@PrefName		VARCHAR(200))
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	SELECT PrefValue FROM smartproject_SystemPreference WHERE PrefName = @PrefName AND [Active] = 1;
END

' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[smartproject_UpdateSystemPreference]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'-- =============================================
-- Author:		David Lee
-- Create date: 11/2/2009
-- Description:	Update System Preference
-- =============================================
CREATE PROCEDURE [dbo].[smartproject_UpdateSystemPreference]
	-- Add the parameters for the stored procedure here
(@PrefName		VARCHAR(200)
,@PrefValue		VARCHAR(200))
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    UPDATE smartproject_SystemPreference
		SET PrefValue = @PrefValue
	WHERE PrefName = @PrefName AND [Active] = 1;
END

' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[smartproject_RemoveSystemPreference]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'-- =============================================
-- Author:		David Lee
-- Create date: 11/2/2009
-- Description:	Remove System Preference
-- =============================================
CREATE PROCEDURE [dbo].[smartproject_RemoveSystemPreference]
	-- Add the parameters for the stored procedure here
(@PrefName		VARCHAR(200))
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    UPDATE smartproject_SystemPreference
		SET [Active] = 0
	WHERE PrefName = @PrefName;
END

' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[smartproject_AddWorkHistory]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[smartproject_AddWorkHistory]
(@ActivityCode	char(3)
,@ByOwnerId		int
,@ToDeveloperId	int)
AS
	INSERT INTO [smartproject_WorkHistory] ([ActivityCode], [ByOwnerId], [ToDeveloperId], [Created])
	VALUES (@ActivityCode, @ByOwnerId, @ToDeveloperId, CURRENT_TIMESTAMP);
	RETURN @@IDENTITY;

' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[smartproject_AddUser]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[smartproject_AddUser]
(@FirstName	varchar(200)
,@LastName	varchar(150)
,@EMail		varchar(250)
,@UserName	varchar(300))
AS
	SET NOCOUNT ON;
	INSERT INTO [smartproject_User] ([FirstName], [LastName], [EMail], [UserName]) 
	VALUES (@FirstName, @LastName, @EMail, @UserName);
	RETURN @@IDENTITY;




' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[smartproject_IsNewInstall]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'-- =============================================
-- Author:		David Lee
-- Create date: 10/28/2009
-- Description:	Determines whether this is a brand new install.
-- =============================================
CREATE PROCEDURE [dbo].[smartproject_IsNewInstall]
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	SELECT COUNT(UserId) FROM [smartproject_User] WHERE [Active] = 1;
END

' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[smartproject_GetUserIDByUserName]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'-- =============================================
-- Author:		David Lee
-- Create date: 11/08/2009
-- Description:	Get user''s ID
-- =============================================
CREATE PROCEDURE [dbo].[smartproject_GetUserIDByUserName]
(@UserName	VarChar(300))
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	SELECT [UserID] FROM [smartproject_User] WHERE [UserName] = @UserName
END

' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[smartproject_GetUserProfile]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'-- =============================================
-- Author:		David Lee
-- Create date: 11/08/2009
-- Description:	Get user Profile
-- =============================================
CREATE PROCEDURE [dbo].[smartproject_GetUserProfile]
(@UserName	VarChar(300))
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	SELECT [UserID],[FirstName], [LastName], [EMail] FROM [smartproject_User] WHERE [UserName] = @UserName
	AND [Active] = 1;
END

' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[smartproject_FindActiveUsers]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[smartproject_FindActiveUsers]
AS
	SET NOCOUNT ON;
		SELECT [UserId], [FirstName], [LastName], [EMail] FROM [smartproject_User]
	WHERE [Active] = 1;
	RETURN


' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[smartproject_AddLog]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'-- =============================================
-- Author:		David Lee
-- Create date: 11/17/2009
-- Description:	Adds a new log message into the log table.
-- =============================================
CREATE PROCEDURE [dbo].[smartproject_AddLog]
	-- Add the parameters for the stored procedure here
           (@PageOrClassName varchar(50)
           ,@ActionType char(3)
           ,@Description varchar(500)
		   ,@SeverityLevel char(3))
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	INSERT INTO [SmartProject].[dbo].[smartproject_Log]
           ([PageOrClassName]
           ,[ActionType]
           ,[Description]
		   ,[SeverityLevel]
           ,[Created])
     VALUES
           (@PageOrClassName
           ,@ActionType
           ,@Description
		   ,@SeverityLevel
           ,CURRENT_TIMESTAMP)
END


' 
END
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[ProductId_WorkProductId]') AND parent_object_id = OBJECT_ID(N'[dbo].[smartproject_Work]'))
ALTER TABLE [dbo].[smartproject_Work]  WITH CHECK ADD  CONSTRAINT [ProductId_WorkProductId] FOREIGN KEY([ProductId])
REFERENCES [dbo].[smartproject_Product] ([ProductId])
GO
ALTER TABLE [dbo].[smartproject_Work] CHECK CONSTRAINT [ProductId_WorkProductId]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[SprintId_WorkSprintId]') AND parent_object_id = OBJECT_ID(N'[dbo].[smartproject_Work]'))
ALTER TABLE [dbo].[smartproject_Work]  WITH CHECK ADD  CONSTRAINT [SprintId_WorkSprintId] FOREIGN KEY([SprintId])
REFERENCES [dbo].[smartproject_Sprint] ([SprintId])
GO
ALTER TABLE [dbo].[smartproject_Work] CHECK CONSTRAINT [SprintId_WorkSprintId]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[UserId_WorkDeveloperId]') AND parent_object_id = OBJECT_ID(N'[dbo].[smartproject_Work]'))
ALTER TABLE [dbo].[smartproject_Work]  WITH CHECK ADD  CONSTRAINT [UserId_WorkDeveloperId] FOREIGN KEY([DeveloperId])
REFERENCES [dbo].[smartproject_User] ([UserId])
GO
ALTER TABLE [dbo].[smartproject_Work] CHECK CONSTRAINT [UserId_WorkDeveloperId]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[UserId_WorkOwnerId]') AND parent_object_id = OBJECT_ID(N'[dbo].[smartproject_Work]'))
ALTER TABLE [dbo].[smartproject_Work]  WITH CHECK ADD  CONSTRAINT [UserId_WorkOwnerId] FOREIGN KEY([OwnerId])
REFERENCES [dbo].[smartproject_User] ([UserId])
GO
ALTER TABLE [dbo].[smartproject_Work] CHECK CONSTRAINT [UserId_WorkOwnerId]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[UserId_WorkHistoryByOwnerId]') AND parent_object_id = OBJECT_ID(N'[dbo].[smartproject_WorkHistory]'))
ALTER TABLE [dbo].[smartproject_WorkHistory]  WITH CHECK ADD  CONSTRAINT [UserId_WorkHistoryByOwnerId] FOREIGN KEY([ByOwnerId])
REFERENCES [dbo].[smartproject_User] ([UserId])
GO
ALTER TABLE [dbo].[smartproject_WorkHistory] CHECK CONSTRAINT [UserId_WorkHistoryByOwnerId]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[UserId_WorkHistoryToDeveloperId]') AND parent_object_id = OBJECT_ID(N'[dbo].[smartproject_WorkHistory]'))
ALTER TABLE [dbo].[smartproject_WorkHistory]  WITH CHECK ADD  CONSTRAINT [UserId_WorkHistoryToDeveloperId] FOREIGN KEY([ToDeveloperId])
REFERENCES [dbo].[smartproject_User] ([UserId])
GO
ALTER TABLE [dbo].[smartproject_WorkHistory] CHECK CONSTRAINT [UserId_WorkHistoryToDeveloperId]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[WorkId_WorkHistoryWorkId]') AND parent_object_id = OBJECT_ID(N'[dbo].[smartproject_WorkHistory]'))
ALTER TABLE [dbo].[smartproject_WorkHistory]  WITH CHECK ADD  CONSTRAINT [WorkId_WorkHistoryWorkId] FOREIGN KEY([WorkId])
REFERENCES [dbo].[smartproject_Work] ([WorkId])
GO
ALTER TABLE [dbo].[smartproject_WorkHistory] CHECK CONSTRAINT [WorkId_WorkHistoryWorkId]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[UserId_SprintOwnerId]') AND parent_object_id = OBJECT_ID(N'[dbo].[smartproject_Sprint]'))
ALTER TABLE [dbo].[smartproject_Sprint]  WITH CHECK ADD  CONSTRAINT [UserId_SprintOwnerId] FOREIGN KEY([OwnerId])
REFERENCES [dbo].[smartproject_User] ([UserId])
GO
ALTER TABLE [dbo].[smartproject_Sprint] CHECK CONSTRAINT [UserId_SprintOwnerId]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[UserId_ProductOwnerId]') AND parent_object_id = OBJECT_ID(N'[dbo].[smartproject_Product]'))
ALTER TABLE [dbo].[smartproject_Product]  WITH CHECK ADD  CONSTRAINT [UserId_ProductOwnerId] FOREIGN KEY([OwnerId])
REFERENCES [dbo].[smartproject_User] ([UserId])
GO
ALTER TABLE [dbo].[smartproject_Product] CHECK CONSTRAINT [UserId_ProductOwnerId]